package com.cee.admin.listener;

import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.DefaultConverterLoader;
import com.cee.common.easyexcel.conveter.BigDecimalNumberConverter;
import com.cee.common.easyexcel.conveter.BigDecimalStringConverter;
import com.cee.common.easyexcel.conveter.ChineseBooleanConverter;
import com.cee.common.exception.GlobalException;
import com.cee.common.spring.SpringContextHolder;
import com.cee.ds.common.service.CarbonSinkProjBaseService;
import com.cee.ds.common.utils.ForestManageUtils;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;


/**
 * @author wsz
 * @since 2024/1/26
 */
@Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 检查碳汇-森林经营计算工具类
        ForestManageUtils.check();

        // 碳资产
        for (CarbonSinkProjBaseService<?> carbonSinkProjService : SpringContextHolder.getBeanList(CarbonSinkProjBaseService.class)) {
            if (CarbonSinkProjBaseService.carbonSinkServiceMap.put(carbonSinkProjService.typeEnum(), carbonSinkProjService) != null) {
                throw new GlobalException("carbonSinkProjService has duplicates,  carbonSinkProjService.getClass() =" + carbonSinkProjService.getClass());
            }
        }


        // easyExcel
        ChineseBooleanConverter chineseBooleanConverter = new ChineseBooleanConverter();
        BigDecimalNumberConverter bigDecimalNumberConverter = new BigDecimalNumberConverter();
        BigDecimalStringConverter bigDecimalStringConverter = new BigDecimalStringConverter();
        DefaultConverterLoader.loadAllConverter().put(ConverterKeyBuild.buildKey(chineseBooleanConverter.supportJavaTypeKey(), chineseBooleanConverter.supportExcelTypeKey()), chineseBooleanConverter);
        DefaultConverterLoader.loadAllConverter().put(ConverterKeyBuild.buildKey(bigDecimalNumberConverter.supportJavaTypeKey(), bigDecimalNumberConverter.supportExcelTypeKey()), bigDecimalNumberConverter);
        DefaultConverterLoader.loadAllConverter().put(ConverterKeyBuild.buildKey(bigDecimalStringConverter.supportJavaTypeKey(), bigDecimalStringConverter.supportExcelTypeKey()), bigDecimalStringConverter);
    }

}
